package gem.model.test;

import gem.model.course.Course;
import gem.model.course.Department;
import gem.model.course.ScienceDomain;

import java.awt.BorderLayout;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.JComboBox;
import java.awt.GridBagConstraints;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class CourseFrame extends JFrame
{

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel pnl_Courses = null;
	private JComboBox cb_Departments = null;
	private JComboBox cb_ScienceDomains = null;
	private JComboBox cb_Courses = null;
	
	private java.util.HashMap<String, Department > depsOLD;// = gem.database.ReaderWriter.loadDepartmentsFully();  //  @jve:decl-index=0:
	private java.util.HashMap<String, ScienceDomain> m_CurrScienceDomainOLD;
	private java.util.HashMap<String, Course> m_CurrCoursesOLD;
	
	private java.util.Vector<Department> deps;  //  @jve:decl-index=0:
	private java.util.Vector<ScienceDomain>m_CurrScienceDomains;
	private java.util.Vector<Course>m_CurrCourses;
	
	

	/**
	 * This is the default constructor
	 */
	public CourseFrame() {
		super();
		initialize();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void setDepartments(java.util.HashMap<String, Department > d )
	{
		depsOLD = d;
		setComponents();
	}
	public void setDepartments( java.util.Vector< Department > d )
	{
		deps = d;
		setComponents();
	}
	private void setComponents()
	{
		for( Department d : deps ) 
		{
			cb_Departments.addItem( d );
		}
		
		cb_Departments.addActionListener( new ActionListener()
		{		
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				cb_ScienceDomains.removeAllItems();
			
				m_CurrScienceDomains = ((Department)cb_Departments.getSelectedItem()).getScienceDomains();
				
				//m_CurrScienceDomainOLD = depsOLD.get( (String)cb_Departments.getSelectedItem() ).getScienceDomains();
				
				if( m_CurrScienceDomains == null )
				{
					return;
				}
				for( ScienceDomain sd : m_CurrScienceDomains )
				{
					cb_ScienceDomains.addItem( sd );
					System.out.println( sd );
				}
				//cb_ScienceDomains.setSelectedIndex(0);
			}
		});
		
		cb_ScienceDomains.addActionListener( new ActionListener()
		{			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				cb_Courses.removeAllItems();
				if( m_CurrScienceDomains == null )
				{
					return;
				}
				if( cb_ScienceDomains.getSelectedIndex() < 0 )
				{
					return;
				}
				m_CurrCourses = ( (ScienceDomain)cb_ScienceDomains.getSelectedItem()).getCourses();
				if( m_CurrCourses == null)
				{
					return;
				}
				for( Course c : m_CurrCourses )
				{
					cb_Courses.addItem(c);
				}
				//cb_Courses.setSelectedIndex(0);
			}
		});
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(625, 264);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getPnl_Courses(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes pnl_Courses	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnl_Courses() {
		if (pnl_Courses == null) {
			pnl_Courses = new JPanel();
			pnl_Courses.setLayout(new FlowLayout());
			pnl_Courses.add(getCb_Departments(), null);
			pnl_Courses.add(getCb_ScienceDomains(), null);
			pnl_Courses.add(getCb_Courses(), null);
		}
		return pnl_Courses;
	}

	/**
	 * This method initializes cb_Departments	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getCb_Departments() {
		if (cb_Departments == null) {
			cb_Departments = new JComboBox();
			cb_Departments.setPreferredSize(new Dimension(400, 25));
		}
		return cb_Departments;
	}

	/**
	 * This method initializes cb_ScienceDomains	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getCb_ScienceDomains() {
		if (cb_ScienceDomains == null) {
			cb_ScienceDomains = new JComboBox();
			cb_ScienceDomains.setPreferredSize(new Dimension(400, 25));
		}
		return cb_ScienceDomains;
	}

	/**
	 * This method initializes cb_Courses	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getCb_Courses() {
		if (cb_Courses == null) {
			cb_Courses = new JComboBox();
			cb_Courses.setPreferredSize(new Dimension(400, 25));
		}
		return cb_Courses;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
